import md5 from "./md5.js"
const config={
  apiUrl:'https://101.5abh.com/',  //当前接口域名
  api: { //接口头参数生成所需key
    key:'CAR2019ANYIDC'
  },
  apihead: {  //接口头参数
    version: '1.0',
    token: '',
    type: 'min',
    noncestr: '',
    did: '',
    sign: ''
  },//接口头配置参数
  apiheader:function() {  //接口头数据处理
    this.apihead.noncestr = md5(Date.parse(new Date().toString()).toString());
    this.apihead.did = Date.parse(new Date().toString()).toString();
    this.apihead.sign = md5(md5('did=' + this.apihead.did + '&noncestr=' + this.apihead.noncestr + '&token=' + this.apihead.token + '&type=' + this.apihead.type + '&version=' + this.apihead.version) + this.api.key);
    return this.apihead;
  },
  gethttp:function(url,data,success){
    wx.request({
      url: url,
      data:data,
      header:this.apiheader,
      method:'get',
      success: function(res){
        if(res.data.status==1){
          const ress=res.data
          success(res);
        } else if (res.data.status == 509) {
          wx.showToast({
            title: '登录信息错误/n即将重新登录',
            icon: 'none'
          })
          wx.switchTab({
            url: '/pages/index/index',
          })
        }else{
          wx.showToast({
            title: res.data.message,
            icon: 'none',
          })
        }
        
      },
      fail:function(){
       
      }
    })
  },
  posthttp: function (url, data, success) {
    wx.request({
      url:this.apiUrl+url,
      data: data,
      header: this.apiheader(),
      method: 'post',
      success: function (res) {
        if (res.data.status == 1) {
          const ress = res.data
          success(res.data);
        } else if (res.data.status == 509 || res.data.message =='Please login first'){
          wx.showToast({
            title: '登录态过期,即将前往授权',
            icon: 'none'
          })
          setTimeout(()=>{
            wx.removeStorageSync('phone')
            wx.removeStorageSync('token')
            wx.reLaunch({
              url: '/pages/index/index',
            })
          },1500)   
        }else{
          wx.showToast({
            title: res.data.message,
            icon:'none'
          })
        }   
      },
      fail: function () {
        
      }
    })
  },
  fileupload:function(opt){
    opt.url = config.apiUrl + opt.imgurl;
    opt.header = this.apiheader();
    const tempFilePaths='';
    var chooseImagePromise = new Promise((resolve, reject)=>{
      wx.chooseImage({
        success(res) {
          const tempFilePaths = res.tempFilePaths
          opt.filePath = tempFilePaths[0];
          wx.showLoading({
            title: '图片加载中...',
          })
          wx.uploadFile(opt);
          resolve(tempFilePaths)
        },fail(res){
          reject(res)
        }
      })
    })
    return chooseImagePromise;
  }
}
module.exports = config;